package com.lq.ecoldchain.internet

import com.lq.ecoldchain.util.toast
import io.reactivex.Observer
import io.reactivex.disposables.Disposable

/**
 * Created by 刘清
 * on 2018/7/4.
 * on 17:17
 */
abstract class BaseObserver<T> : Observer<ResultEntity<T>> {
    override fun onSubscribe(d: Disposable) {}

    override fun onNext(result: ResultEntity<T>) {
        if(!result.msg.isNullOrEmpty()){
            toast { result.msg!! }
        }
        if (result.code == 200) {
            onSuccess(result.data)
        } else {
            onFail(result.msg!!)
        }
    }

    override fun onError(e: Throwable) {
        val t = ExceptionHandle.handleException(e).message
        if (!t.isNullOrEmpty()) {
            toast {t}
            onFail(t)
        }
    }

    override fun onComplete() {}

    protected abstract fun onSuccess(t: T?)

    protected abstract fun onFail(code: String)
}
